{
 "metadata": {},
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "100 numpy exercises"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "The goal is both to offer a quick reference for new and old users and to\n",
      "provide also a set of exercices for those who teach. If you remember\n",
      "having asked or answered a (short) problem, you can send a pull request.\n",
      "The format is:\n"
     ]
    },
    {
     "cell_type": "raw",
     "metadata": {},
     "source": [
      "#. Find indices of non-zero elements from [1,2,0,0,4,0]\n",
      "\n",
      "   .. code:: python\n",
      "\n",
      "      # Author: Somebody\n",
      "\n",
      "      print np.nonzero([1,2,0,0,4,0])"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Here is what the page looks like so far:\n",
      "<http://www.loria.fr/~rougier/teaching/numpy.100/index.html>\n"
     ]
    },
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Note"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "The level names came from an old-game (Dungeon Master)\n"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Repository is at: <https://github.com/rougier/numpy-100>\n"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "The corresponding [IPython\n",
      "notebook](https://github.com/rougier/numpy-100/blob/master/README.ipynb)\n",
      "is available from the github repo, thanks to the\n",
      "[rst2ipynb](https://github.com/esc/rst2ipynb) conversion tool by\n",
      "[Valentin Haenel](http://haenel.co)\n"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Thanks to Michiaki Ariga, there is now a [Julia\n",
      "version](https://github.com/chezou/julia-100-exercises).\n"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Neophyte"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Import the numpy package under the name `np`\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import numpy as np"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Print the numpy version and the configuration.\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print np.__version__\n",
      "np.__config__.show()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Create a null vector of size 10\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "Z = np.zeros(10)\n",
      "print Z"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Create a null vector of size 10 but the fifth value which is 1\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "Z = np.zeros(10)\n",
      "Z[4] = 1\n",
      "print Z"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Create a vector with values ranging from 10 to 49\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "Z = np.arange(10,50)\n",
      "print Z"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Create a 3x3 matrix with values ranging from 0 to 8\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "Z = np.arange(9).reshape(3,3)\n",
      "print Z"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Find indices of non-zero elements from [1,2,0,0,4,0]\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "nz = np.nonzero([1,2,0,0,4,0])\n",
      "print nz"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Create a 3x3 identity matrix\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "Z = np.eye(3)\n",
      "print Z"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Create a 5x5 matrix with values 1,2,3,4 just below the diagonal\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "Z = np.diag(1+np.arange(4),k=-1)\n",
      "print Z"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Create a 3x3x3 array with random values\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "Z = np.random.random((3,3,3))\n",
      "print Z"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Novice"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Create a 8x8 matrix and fill it with a checkerboard pattern\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "Z = np.zeros((8,8),dtype=int)\n",
      "Z[1::2,::2] = 1\n",
      "Z[::2,1::2] = 1\n",
      "print Z"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Create a 10x10 array with random values and find the minimum and maximum\n",
      "values\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "Z = np.random.random((10,10))\n",
      "Zmin, Zmax = Z.min(), Z.max()\n",
      "print Zmin, Zmax"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Create a checkerboard 8x8 matrix using the tile function\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "Z = np.tile( np.array([[0,1],[1,0]]), (4,4))\n",
      "print Z"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Normalize a 5x5 random matrix (between 0 and 1)\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "Z = np.random.random((5,5))\n",
      "Zmax,Zmin = Z.max(), Z.min()\n",
      "Z = (Z - Zmin)/(Zmax - Zmin)\n",
      "print Z"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Multiply a 5x3 matrix by a 3x2 matrix (real matrix product)\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "Z = np.dot(np.ones((5,3)), np.ones((3,2)))\n",
      "print Z"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Create a 5x5 matrix with row values ranging from 0 to 4\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "Z = np.zeros((5,5))\n",
      "Z += np.arange(5)\n",
      "print Z"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Create a vector of size 10 with values ranging from 0 to 1, both\n",
      "excluded\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "Z = np.linspace(0,1,12,endpoint=True)[1:-1]\n",
      "print Z"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Create a random vector of size 10 and sort it\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "Z = np.random.random(10)\n",
      "Z.sort()\n",
      "print Z"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Consider two random array A anb B, check if they are equal.\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "A = np.random.randint(0,2,5)\n",
      "B = np.random.randint(0,2,5)\n",
      "equal = np.allclose(A,B)\n",
      "print equal"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Create a random vector of size 30 and find the mean value\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "Z = np.random.random(30)\n",
      "m = Z.mean()\n",
      "print m"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Apprentice"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Make an array immutable (read-only)\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "Z = np.zeros(10)\n",
      "Z.flags.writeable = False\n",
      "Z[0] = 1"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Consider a random 10x2 matrix representing cartesian coordinates,\n",
      "convert them to polar coordinates\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "Z = np.random.random((10,2))\n",
      "X,Y = Z[:,0], Z[:,1]\n",
      "R = np.sqrt(X**2+Y**2)\n",
      "T = np.arctan2(Y,X)\n",
      "print R\n",
      "print T"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Create random vector of size 10 and replace the maximum value by 0\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "Z = np.random.random(10)\n",
      "Z[Z.argmax()] = 0\n",
      "print Z"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Create a structured array with `x` and `y` coordinates covering the\n",
      "[0,1]x[0,1] area.\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "Z = np.zeros((10,10), [('x',float),('y',float)])\n",
      "Z['x'], Z['y'] = np.meshgrid(np.linspace(0,1,10),\n",
      "                             np.linspace(0,1,10))\n",
      "print Z"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Print the minimum and maximum representable value for each numpy scalar\n",
      "type\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "for dtype in [np.int8, np.int32, np.int64]:\n",
      "   print np.iinfo(dtype).min\n",
      "   print np.iinfo(dtype).max\n",
      "for dtype in [np.float32, np.float64]:\n",
      "   print np.finfo(dtype).min\n",
      "   print np.finfo(dtype).max\n",
      "   print np.finfo(dtype).eps"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Create a structured array representing a position (x,y) and a color\n",
      "(r,g,b)\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      " Z = np.zeros(10, [ ('position', [ ('x', float, 1),\n",
      "                                   ('y', float, 1)]),\n",
      "                    ('color',    [ ('r', float, 1),\n",
      "                                   ('g', float, 1),\n",
      "                                   ('b', float, 1)])])\n",
      "print Z"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Consider a random vector with shape (100,2) representing coordinates,\n",
      "find point by point distances\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "Z = np.random.random((10,2))\n",
      "X,Y = np.atleast_2d(Z[:,0]), np.atleast_2d(Z[:,1])\n",
      "D = np.sqrt( (X-X.T)**2 + (Y-Y.T)**2)\n",
      "print D\n",
      "\n",
      "# Much faster with scipy\n",
      "import scipy\n",
      "Z = np.random.random((10,2))\n",
      "D = scipy.spatial.distance.cdist(Z,Z)\n",
      "print D"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Generate a generic 2D Gaussian-like array\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "X, Y = np.meshgrid(np.linspace(-1,1,10), np.linspace(-1,1,10))\n",
      "D = np.sqrt(X*X+Y*Y)\n",
      "sigma, mu = 1.0, 0.0\n",
      "G = np.exp(-( (D-mu)**2 / ( 2.0 * sigma**2 ) ) )\n",
      "print G"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "How to tell if a given 2D array has null columns ?\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Author: Warren Weckesser\n",
      "\n",
      "Z = np.random.randint(0,3,(3,10))\n",
      "print (~Z.any(axis=0)).any()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Find the nearest value from a given value in an array\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "Z = np.random.uniform(0,1,10)\n",
      "z = 0.5\n",
      "m = Z.flat[np.abs(Z - z).argmin()]\n",
      "print m"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Journeyman"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Consider the following file:\n"
     ]
    },
    {
     "cell_type": "raw",
     "metadata": {},
     "source": [
      "1,2,3,4,5\n",
      "6,,,7,8\n",
      ",,9,10,11"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "How to read it ?\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "Z = np.genfromtxt(\"missing.dat\", delimiter=\",\")"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Consider a generator function that generates 10 integers and use it to\n",
      "build an array\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def generate():\n",
      "    for x in xrange(10):\n",
      "        yield x\n",
      "Z = np.fromiter(generate(),dtype=float,count=-1)\n",
      "print Z"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Consider a given vector, how to add 1 to each element indexed by a\n",
      "second vector (be careful with repeated indices) ?\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Author: Brett Olsen\n",
      "\n",
      "Z = np.ones(10)\n",
      "I = np.random.randint(0,len(Z),20)\n",
      "Z += np.bincount(I, minlength=len(Z))\n",
      "print Z"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "How to accumulate elements of a vector (X) to an array (F) based on an\n",
      "index list (I) ?\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Author: Alan G Isaac\n",
      "\n",
      "X = [1,2,3,4,5,6]\n",
      "I = [1,3,9,3,4,1]\n",
      "F = np.bincount(I,X)\n",
      "print F"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Considering a (w,h,3) image of (dtype=ubyte), compute the number of\n",
      "unique colors\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Author: Nadav Horesh\n",
      "\n",
      "w,h = 16,16\n",
      "I = np.random.randint(0,2,(h,w,3)).astype(np.ubyte)\n",
      "F = I[...,0]*256*256 + I[...,1]*256 +I[...,2]\n",
      "n = len(np.unique(F))\n",
      "print np.unique(I)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Considering a four dimensions array, how to get sum over the last two\n",
      "axis at once ?\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "A = np.random.randint(0,10,(3,4,3,4))\n",
      "sum = A.reshape(A.shape[:-2] + (-1,)).sum(axis=-1)\n",
      "print"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Considering a one-dimensional vector D, how to compute means of subsets\n",
      "of D using a vector S of same size describing subset indices ?\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Author: Jaime Fern\u00e1ndez del R\u00edo\n",
      "\n",
      "D = np.random.uniform(0,1,100)\n",
      "S = np.random.randint(0,10,100)\n",
      "D_sums = np.bincount(S, weights=D)\n",
      "D_counts = np.bincount(S)\n",
      "D_means = D_sums / D_counts\n",
      "print D_means"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Consider the vector [1, 2, 3, 4, 5], how to build a new vector with 3\n",
      "consecutive zeros interleaved between each value ?\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Author: Warren Weckesser\n",
      "\n",
      "Z = np.array([1,2,3,4,5])\n",
      "nz = 3\n",
      "Z0 = np.zeros(len(Z) + (len(Z)-1)*(nz))\n",
      "Z0[::nz+1] = Z\n",
      "print Z0"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Consider an array of dimension (5,5,3), how to mulitply it by an array\n",
      "with dimensions (5,5) ?\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "A = np.ones((5,5,3))\n",
      "B = 2*np.ones((5,5))\n",
      "print A * B[:,:,None]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "How to swap two rows of an array ?\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Author: Eelco Hoogendoorn\n",
      "\n",
      "A = np.arange(25).reshape(5,5)\n",
      "A[[0,1]] = A[[1,0]]\n",
      "print A"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Craftsman"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Consider a one-dimensional array Z, build a two-dimensional array whose\n",
      "first row is (Z[0],Z[1],Z[2]) and each subsequent row is shifted by 1\n",
      "(last row should be (Z[-3],Z[-2],Z[-1])\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Author: Joe Kington / Erik Rigtorp\n",
      "from numpy.lib import stride_tricks\n",
      "\n",
      "def rolling(a, window):\n",
      "    shape = (a.size - window + 1, window)\n",
      "    strides = (a.itemsize, a.itemsize)\n",
      "    return stride_tricks.as_strided(a, shape=shape, strides=strides)\n",
      "Z = rolling(np.arange(10), 3)\n",
      "print Z"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Consider a set of 10 triplets describing 10 triangles (with shared\n",
      "vertices), find the set of unique line segments composing all the\n",
      "triangles.\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Author: Nicolas P. Rougier\n",
      "\n",
      "faces = np.random.randint(0,100,(10,3))\n",
      "F = np.roll(faces.repeat(2,axis=1),-1,axis=1)\n",
      "F = F.reshape(len(F)*3,2)\n",
      "F = np.sort(F,axis=1)\n",
      "G = F.view( dtype=[('p0',F.dtype),('p1',F.dtype)] )\n",
      "G = np.unique(G)\n",
      "print G"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Given an array C that is a bincount, how to produce an array A such that\n",
      "np.bincount(A) == C ?\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Author: Jaime Fern\u00e1ndez del R\u00edo\n",
      "\n",
      "C = np.bincount([1,1,2,3,4,4,6])\n",
      "A = np.repeat(np.arange(len(C)), C)\n",
      "print A"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "How to compute averages using a sliding window over an array ?\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Author: Jaime Fern\u00e1ndez del R\u00edo\n",
      "\n",
      "def moving_average(a, n=3) :\n",
      "    ret = np.cumsum(a, dtype=float)\n",
      "    ret[n:] = ret[n:] - ret[:-n]\n",
      "    return ret[n - 1:] / n\n",
      "Z = np.arange(20)\n",
      "print moving_average(Z, n=3)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Artisan"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Considering a 10x3 matrix, extract rows with unequal values (e.g.\n",
      "[2,2,3])\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Author: Robert Kern\n",
      "\n",
      "Z = np.random.randint(0,5,(10,3))\n",
      "E = np.logical_and.reduce(Z[:,1:] == Z[:,:-1], axis=1)\n",
      "U = Z[~E]\n",
      "print Z\n",
      "print U"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Convert a vector of ints into a matrix binary representation.\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Author: Warren Weckesser\n",
      "\n",
      "I = np.array([0, 1, 2, 3, 15, 16, 32, 64, 128])\n",
      "B = ((I.reshape(-1,1) & (2**np.arange(8))) != 0).astype(int)\n",
      "print B[:,::-1]\n",
      "\n",
      "# Author: Daniel T. McDonald\n",
      "\n",
      "I = np.array([0, 1, 2, 3, 15, 16, 32, 64, 128], dtype=np.uint8)\n",
      "print np.unpackbits(I[:, np.newaxis], axis=1)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Adept"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Consider an arbitrary array, write a function that extract a subpart\n",
      "with a fixed shape and centered on a given element (pad with a `fill`\n",
      "value when necessary)\n"
     ]
    },
    {
     "cell_type": "raw",
     "metadata": {},
     "source": [
      "# Author: Nicolas Rougier\n",
      "\n",
      "Z = np.random.randint(0,10,(10,10))\n",
      "shape = (5,5)\n",
      "fill  = 0\n",
      "position = (1,1)\n",
      "\n",
      "R = np.ones(shape, dtype=Z.dtype)*fill\n",
      "P  = np.array(list(position)).astype(int)\n",
      "Rs = np.array(list(R.shape)).astype(int)\n",
      "Zs = np.array(list(Z.shape)).astype(int)\n",
      "\n",
      "R_start = np.zeros((len(shape),)).astype(int)\n",
      "R_stop  = np.array(list(shape)).astype(int)\n",
      "Z_start = (P-Rs//2)\n",
      "Z_stop  = (P+Rs//2)+Rs%2\n",
      "\n",
      "R_start = (R_start - np.minimum(Z_start,0)).tolist()\n",
      "Z_start = (np.maximum(Z_start,0)).tolist()\n",
      "R_stop = np.maximum(R_start, (R_stop - np.maximum(Z_stop-Zs,0))).tolist()\n",
      "Z_stop = (np.minimum(Z_stop,Zs)).tolist()\n",
      "\n",
      "r = [slice(start,stop) for start,stop in zip(R_start,R_stop)]\n",
      "z = [slice(start,stop) for start,stop in zip(Z_start,Z_stop)]\n",
      "R[r] = Z[z]\n",
      "print Z\n",
      "print R"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Consider an array Z = [1,2,3,4,5,6,7,8,9,10,11,12,13,14], how to\n",
      "generate an array R = [[1,2,3,4], [2,3,4,5], [3,4,5,6], ...,\n",
      "[11,12,13,14]] ?\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Author: St\u00e9fan van der Walt\n",
      "\n",
      "Z = np.arange(1,15,dtype=uint32)\n",
      "R = stride_tricks.as_strided(Z,(11,4),(4,4))\n",
      "print R"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Expert"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Consider two arrays A and B of shape (8,3) and (2,2). How to find rows\n",
      "of A that contain elements of each row of B regardless of the order of\n",
      "the elements in B ?\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Author: Gabe Schwartz\n",
      "\n",
      "A = np.random.randint(0,5,(8,3))\n",
      "B = np.random.randint(0,5,(2,2))\n",
      "\n",
      "C = (A[..., np.newaxis, np.newaxis] == B)\n",
      "rows = (C.sum(axis=(1,2,3)) >= B.shape[1]).nonzero()[0]\n",
      "print rows"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Extract all the contiguous 3x3 blocks from a random 10x10 matrix.\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Author: Chris Barker\n",
      "\n",
      "Z = np.random.randint(0,5,(10,10))\n",
      "n = 3\n",
      "i = 1 + (Z.shape[0]-3)\n",
      "j = 1 + (Z.shape[1]-3)\n",
      "C = stride_tricks.as_strided(Z, shape=(i, j, n, n), strides=Z.strides + Z.strides)\n",
      "print C"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Create a 2D array subclass such that Z[i,j] == Z[j,i]\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Author: Eric O. Lebigot\n",
      "# Note: only works for 2d array and value setting using indices\n",
      "\n",
      "class Symetric(np.ndarray):\n",
      "    def __setitem__(self, (i,j), value):\n",
      "        super(Symetric, self).__setitem__((i,j), value)\n",
      "        super(Symetric, self).__setitem__((j,i), value)\n",
      "\n",
      "def symetric(Z):\n",
      "    return np.asarray(Z + Z.T - np.diag(Z.diagonal())).view(Symetric)\n",
      "\n",
      "S = symetric(np.random.randint(0,10,(5,5)))\n",
      "S[2,3] = 42\n",
      "print S"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Consider a set of p matrices wich shape (n,n) and a set of p vectors\n",
      "with shape (n,1). How to compute the sum of of the p matrix products at\n",
      "once ? (result has shape (n,1))\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Author: St\u00e9fan van der Walt\n",
      "\n",
      "p, n = 10, 20\n",
      "M = np.ones((p,n,n))\n",
      "V = np.ones((p,n,1))\n",
      "S = np.tensordot(M, V, axes=[[0, 2], [0, 1]])\n",
      "print S\n",
      "\n",
      "# It works, because:\n",
      "# M is (p,n,n)\n",
      "# V is (p,n,1)\n",
      "# Thus, summing over the paired axes 0 and 0 (of M and V independently),\n",
      "# and 2 and 1, to remain with a (n,1) vector."
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Master"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Given a two dimensional array, how to extract unique rows ?\n"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Note"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "See\n",
      "[stackoverflow](http://stackoverflow.com/questions/16970982/find-unique-rows-in-numpy-array/)\n",
      "for explanations.\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Author: Jaime Fern\u00e1ndez del R\u00edo\n",
      "\n",
      "Z = np.random.randint(0,2,(6,3))\n",
      "T = np.ascontiguousarray(Z).view(np.dtype((np.void, Z.dtype.itemsize * Z.shape[1])))\n",
      "_, idx = np.unique(T, return_index=True)\n",
      "uZ = Z[idx]\n",
      "print uZ"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Archmaster"
     ]
    }
   ],
   "metadata": {}
  }
 ]
}